package fu.abc.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fu.abc.DAO.AccountDAO;
import fu.abc.model.Account;

/**
 * Servlet implementation class CreateAccount
 */
@WebServlet("/CreateAccount")
public class CreateAccount extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateAccount() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		Account acc = new Account();
		AccountDAO manage = new AccountDAO();
		
		String message = "Tạo tài khoản thành công";
		int status = 1;
		
//		get information from jsp
		String fullname = request.getParameter("fullname").toString().trim();
		String username = request.getParameter("username").toString().trim();
		String address = request.getParameter("address").toString().trim();
		String email = request.getParameter("email").toString().trim();
		String password = request.getParameter("password").toString().trim();
		String dob = request.getParameter("dob").toString().trim();
		String cmt = request.getParameter("cmt").toString().trim();
		String phone = request.getParameter("phone").toString().trim();
		String role = request.getParameter("role").toString().trim();
		
				acc.setUsername(username);
				acc.setAddress(address);
				acc.setFullname(fullname);
				acc.setEmail(email);
				acc.setPassword(password);
				acc.setDob(dob);
				acc.setCmt(Integer.parseInt(cmt));
				acc.setPhone(phone);
				acc.setRole(role);
				acc.setStatus(status);
				
				try {
//					call method createAccount
					manage.createAccount(acc);
					request.setAttribute("mess", message);
//					send message success
					RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/systemAdmin/createAccount.jsp");
					dispatcher.forward(request, response);
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			

}
